package com.cy.detectsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.detectsys.entity.TargetInfo;
import com.cy.detectsys.entity.vo.TargetInfoVo;
import com.cy.detectsys.mapper.targetInfoMapper;
import com.cy.detectsys.response.KeyObjectResponse;
import com.cy.detectsys.service.TargetInfoService;
import com.cy.detectsys.mapper.targetInfoMapper;
import com.cy.detectsys.util.BaseResponseInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.List;

/**
* @author asus
* @description 针对表【target_info(重点目标表)】的数据库操作Service实现
* @createDate 2024-06-12 16:40:19
*/
@Service
public class TargetInfoServiceImpl extends ServiceImpl<targetInfoMapper, TargetInfo>
    implements TargetInfoService{

     @Resource
     targetInfoMapper targetInfoMapper;

    /**
     * @description: TODO
     * @author  重点目标的数量
     * @date 2024/6/3 13:51
     * @version 1.0
     */

    @Override
    public BaseResponseInfo KeyObjectList(TargetInfoVo targetInfoVo) {

        //  List<TargetInfo> list =  list();

        if (targetInfoVo.getPageNum() != null && targetInfoVo.getPageSize() != null) {
            PageHelper.startPage(targetInfoVo.getPageNum(), targetInfoVo.getPageSize());

            QueryWrapper<TargetInfo> wrapper = new QueryWrapper<>();

            // 查询目标所属
            if (StringUtils.isNotBlank(targetInfoVo.getTargetOwnership())) {
                wrapper.eq("target_ownership", targetInfoVo.getTargetOwnership());
            }

            // 查询中文名字
            if (StringUtils.isNotBlank(targetInfoVo.getChineseName())) {
                wrapper.eq("chinese_name", targetInfoVo.getChineseName());
            }

            // 查询总部详细地址
            if (StringUtils.isNotBlank(targetInfoVo.getAddress())) {
                wrapper.like("address", targetInfoVo.getAddress());
            }
            //根据id查询
            if (StringUtils.isNotBlank(targetInfoVo.getId())){
                wrapper.eq("id", targetInfoVo.getId());
            }

            List<TargetInfo> list =  list(wrapper);
            // 如果传入了分页参数，则使用 PageInfo 进行封装
            PageInfo<TargetInfo> TargetInfo = new PageInfo<>(list);

            BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
            baseResponseInfo.data = TargetInfo;
            baseResponseInfo.setCode(200);
            baseResponseInfo.setMsg("成功");
            return baseResponseInfo;
        } else {
            // 如果没有传入分页参数，则直接返回列表数据
            BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
            baseResponseInfo.data = list();
            baseResponseInfo.setCode(200);
            baseResponseInfo.setMsg("成功");
            return baseResponseInfo;
        }

//        PageHelper.startPage(TargetInfoVo.getPageNum(), TargetInfoVo.getPageSize());
//        List<TargetInfo> list =  list();
//
//        PageInfo<TargetInfo> TargetInfo = new PageInfo<>(list);
//
//        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
//        baseResponseInfo.data = TargetInfo;
//        baseResponseInfo.setCode(200);
//        baseResponseInfo.setMsg("成功");
//        return baseResponseInfo;
    }

    @Override
    public BaseResponseInfo ownershipAssets() {
        List<TargetInfo> list = targetInfoMapper.ownershipAssets();
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        baseResponseInfo.data = list;
        baseResponseInfo.setCode(200);
        baseResponseInfo.setMsg("成功");
        return baseResponseInfo;
    }

    @Override
    public BaseResponseInfo assets() {
        List<TargetInfo> list = targetInfoMapper.assets();
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        baseResponseInfo.data = list;
        baseResponseInfo.setCode(200);
        baseResponseInfo.setMsg("成功");
        return baseResponseInfo;
    }

    @Override
    public BaseResponseInfo KeyObjectDetails(TargetInfoVo TargetInfoVo) {
        //位置经纬度 年份 所属组织 域名
        List<TargetInfo> list = targetInfoMapper.KeyObjectDetails(TargetInfoVo);
        //ip
        List<TargetInfo> keyObjectDetailsByIp = targetInfoMapper.KeyObjectDetailsByIp(TargetInfoVo);
        //端口
        List<TargetInfo> keyObjectDetailsByPort = targetInfoMapper.KeyObjectDetailsByPort(TargetInfoVo);
        //service
        List<TargetInfo> keyObjectDetailsByService = targetInfoMapper.KeyObjectDetailsByService(TargetInfoVo);
        KeyObjectResponse keyObjectResponse = new KeyObjectResponse();
        // 结果
        TargetInfo TargetInfo=  targetInfoMapper.detectionResult(TargetInfoVo);
        // 独立ip
        List<TargetInfo> TargetInfoList =targetInfoMapper.independentIP(TargetInfoVo);
        //统一封装返回
        keyObjectResponse.setIndependentIP(TargetInfoList.size());
        keyObjectResponse.setDetectionResult(TargetInfo.getDetectionResult());

        keyObjectResponse.setList(list);
        keyObjectResponse.setKeyObjectDetailsByIp(keyObjectDetailsByIp);
        keyObjectResponse.setKeyObjectDetailsByPort(keyObjectDetailsByPort);
        keyObjectResponse.setKeyObjectDetailsByService(keyObjectDetailsByService);

        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        baseResponseInfo.data = keyObjectResponse;
        baseResponseInfo.setCode(200);
        baseResponseInfo.setMsg("成功");
        return baseResponseInfo;
    }

    @Override
    public BaseResponseInfo KeyObjectDetailsByIp(TargetInfoVo TargetInfoVo) {
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        if (TargetInfoVo == null) {
            baseResponseInfo.setCode(400);
            baseResponseInfo.setMsg("参数为空");
            return baseResponseInfo;
        }

        List<TargetInfo> list = targetInfoMapper.KeyObjectDetailsByIp(TargetInfoVo);
        //BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        baseResponseInfo.data = list;
        baseResponseInfo.setCode(200);
        baseResponseInfo.setMsg("成功");
        return baseResponseInfo;
    }

    @Override
    public BaseResponseInfo KeyObjectDetailsByPort(TargetInfoVo TargetInfoVo) {
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        if (TargetInfoVo == null) {
            baseResponseInfo.setCode(400);
            baseResponseInfo.setMsg("参数为空");
            return baseResponseInfo;
        }

        List<TargetInfo> list = targetInfoMapper.KeyObjectDetailsByPort(TargetInfoVo);
        //BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        baseResponseInfo.data = list;
        baseResponseInfo.setCode(200);
        baseResponseInfo.setMsg("成功");
        return baseResponseInfo;

    }

    @Override
    public BaseResponseInfo KeyObjectDetailsByService(TargetInfoVo TargetInfoVo) {
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        if (TargetInfoVo == null) {
            baseResponseInfo.setCode(400);
            baseResponseInfo.setMsg("参数为空");
            return baseResponseInfo;
        }

        List<TargetInfo> list = targetInfoMapper.KeyObjectDetailsByService(TargetInfoVo);
        //BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        baseResponseInfo.data = list;
        baseResponseInfo.setCode(200);
        baseResponseInfo.setMsg("成功");
        return baseResponseInfo;
    }

    @Override
    public BaseResponseInfo KeyObject() {
        List<TargetInfo> list = list();
        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        baseResponseInfo.data = list;
        baseResponseInfo.setCode(200);
        baseResponseInfo.setMsg("成功");
        return baseResponseInfo;
    }

    @Override
    public BaseResponseInfo KeyObjectBox() {
        QueryWrapper<TargetInfo> wrapper = new QueryWrapper<>();
        wrapper.select("DISTINCT target_ownership");
        List<TargetInfo> ownership = targetInfoMapper.selectList(wrapper);

        wrapper.select("DISTINCT home_country");
        List<TargetInfo> homeCountry = targetInfoMapper.selectList(wrapper);
        KeyObjectResponse keyObjectResponse = new KeyObjectResponse();
        keyObjectResponse.setOwnership(ownership);
        keyObjectResponse.setHomeCountry(homeCountry);

        BaseResponseInfo baseResponseInfo = new BaseResponseInfo();
        baseResponseInfo.data = keyObjectResponse;
        baseResponseInfo.setCode(200);
        baseResponseInfo.setMsg("成功");

        return baseResponseInfo;
    }


}




